﻿using QuestionnaireInvestigation.Data.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuestionnaireInvestigation.Data.Service
{
    public class SystemsAnalysisService : IDisposable
    {
        private QuestionnaireInvestigationDBEntities data;
        public SystemsAnalysisService()
        {
            data = new QuestionnaireInvestigationDBEntities();
        }

        public void Dispose()
        {
            data.Dispose();
        }

        public List<int> GetAnswerSituation()
        {
            List<int> result = new List<int>();
            var answer = data.Answers.ToList();
            var currentTime = DateTime.Now;
            result.Add(answer.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Monday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(answer.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Tuesday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(answer.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Wednesday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(answer.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Thursday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(answer.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Friday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            return result;
        }

        public int GetQuestionnaireNumber()
        {
            return data.Questionnaires.ToList().Count;
        }

        public int GetAnswerNumber()
        {
            return data.Answers.ToList().Count;
        }

        public List<int> GetCreateSituation()
        {
            List<int> result = new List<int>();
            var currentTime = DateTime.Now;
            var questionnaire = data.Questionnaires.ToList();
            result.Add(questionnaire.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Monday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(questionnaire.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Tuesday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(questionnaire.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Wednesday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(questionnaire.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Thursday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            result.Add(questionnaire.Where(a => a.CreateTime.DayOfWeek == DayOfWeek.Friday && IsInSameWeek(a.CreateTime, currentTime)).Count());
            return result;
        }
        private bool IsInSameWeek(DateTime dtmS, DateTime dtmE)
        {
            TimeSpan ts = dtmE - dtmS;
            double dbl = ts.TotalDays;
            int intDow = Convert.ToInt32(dtmE.DayOfWeek);
            if (intDow == 0) intDow = 7;
            if (dbl >= 7 || dbl >= intDow) return false;
            else return true;
        }
    }
}
